package e107;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class ReadFile {
	
	public static void read(String path) throws FileNotFoundException{
		try {
			BufferedReader buffer = new BufferedReader(new FileReader(path));
			String line = "";
			StringTokenizer st = null;
			
			int lineNumber = 0, tokenNumber =0;
			
			while( (line = buffer.readLine())  !=  null){
				lineNumber++;	//une ligne de plus
				
				st = new StringTokenizer(line, ",");
				while(st.hasMoreTokens()){
					tokenNumber++;
					
					System.out.println("Line n"+lineNumber+
							", Token n" + tokenNumber +
							", Token: " + st.nextToken());
				}
				tokenNumber = 0;
				
			}
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static int[][] read2(String path) throws FileNotFoundException{
		int[][] graph = new int [(Integer) null][(Integer) null];
		
		try {
			BufferedReader buffer = new BufferedReader(new FileReader(path));
			String line = "";
			StringTokenizer st = null;
			
			int lineNumber = 0, tokenNumber =0;
			
			while( (line = buffer.readLine())  !=  null){
				lineNumber++;	//une ligne de plus
				
				st = new StringTokenizer(line, ",");
				while(st.hasMoreTokens()){
					tokenNumber++;
					
					String next = st.nextToken();
					
					System.out.println("Line n"+lineNumber+
							", Token n" + tokenNumber +
							", Token: " + next);
					
					
					if(next=="-")
						graph[lineNumber][tokenNumber]= (Integer) null;
					else
						graph[lineNumber][tokenNumber]= Integer.parseInt(next);	
					
					tokenNumber = 0;
				}		
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return graph;
	}
	
	public static  ArrayList<Integer[]>  read3(String path) throws FileNotFoundException{
		ArrayList<Integer[]> graph = new ArrayList<Integer[]> ();
		
		try {
			BufferedReader buffer = new BufferedReader(new FileReader(path));
			String line = "";
			StringTokenizer st = null;
			
			int lineNumber = 0, tokenNumber =0;
			
			while( (line = buffer.readLine())  !=  null){
				lineNumber++;	//une ligne de plus
				
				Integer[] row = new Integer[200];
				
				st = new StringTokenizer(line, ",");
				while(st.hasMoreTokens()){
					tokenNumber++;
					
					String next = st.nextToken();
					
					System.out.println("Line n"+lineNumber+
							", Token n" + tokenNumber +
							", Token: " + next);
					
					if(next=="-"){
						//	graph[lineNumber][tokenNumber]= (Integer) null;
						row[tokenNumber] = (Integer) null;
					}else{
						//graph[lineNumber][tokenNumber]= Integer.parseInt(next);	
//						row[tokenNumber]  = Integer.parseInt(next);
//						System.out.println("parse: "+Integer.parseInt(next));
						System.out.println("Next: **"+next+"**");
					}
					tokenNumber = 0;
					
				}		
				
				graph.add(row);
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return graph;
	}
	
	//read 4
	public static  ArrayList<int[]>  read4(String path) throws FileNotFoundException{
		ArrayList<int[]> graph = new ArrayList<int[]> ();
		
		try {
			BufferedReader buffer = new BufferedReader(new FileReader(path));
			String line = "";
			StringTokenizer st = null;
			
			int lineNumber = 0, tokenNumber =0;
			
			while( (line = buffer.readLine())  !=  null){
				lineNumber++;	//une ligne de plus
				
				int[] row = new int[41];
				
				st = new StringTokenizer(line, ",");
				while(st.hasMoreTokens()){
					tokenNumber++;
					
					String next = st.nextToken();
					
					System.out.println("Line n"+lineNumber+
							", Token n" + tokenNumber +
							", Token: " + next);
					
					if(next=="-"){
						//	graph[lineNumber][tokenNumber]= (Integer) null;
						row[tokenNumber] = (Integer) null;
					}else{
						//graph[lineNumber][tokenNumber]= Integer.parseInt(next);	
//						row[tokenNumber]  = Integer.parseInt(next);
//						System.out.println("parse: "+Integer.parseInt(next));
						System.out.println("Next: **"+next+"**");
					}
					tokenNumber = 0;
				}		
				
				graph.add(row);
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return graph;
	}
	
	//read 5
	public static  int [][] read5(String path) throws FileNotFoundException{
		int [][] graph =new int[41][41];
		
		try {
			BufferedReader buffer = new BufferedReader(new FileReader(path));
			String line = "";
			StringTokenizer st = null;
			
			int lineNumber = 0, tokenNumber =0;
			
			while( (line = buffer.readLine())  !=  null){
				lineNumber++;	//une ligne de plus
			
				st = new StringTokenizer(line, ",");
				
				while(st.hasMoreTokens()){
					tokenNumber++;
					
					String next = st.nextToken();
					
					System.out.println("Line n"+lineNumber+", Token n" + tokenNumber +", Token: " + next);
					
					if(next.equalsIgnoreCase("-")){
						//	graph[lineNumber][tokenNumber]= (Integer) null;
						graph [lineNumber][tokenNumber] = 0;
						System.out.println("haut");
					}else{
						System.out.println("bas");
						System.out.println("Next: **"+next+"**");
						graph [lineNumber][tokenNumber] = Integer.parseInt(next);
					}
					tokenNumber = 0;
				}	//end while 2
			}	//end while 1
			
		} catch (IOException e) {

			e.printStackTrace();
		}
		
		return graph;
	}
}
